package com.zw.yzk.kabu.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.EditText;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.data.utils.SharedPrefrenceUtils;
import com.zw.yzk.kabu.internal.component.DaggerLoginComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.LoginModule;
import com.zw.yzk.kabu.jpush.JPushHelper;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.presenter.LoginPresenter;
import com.zw.yzk.kabu.utils.GetRsaString;
import com.zw.yzk.kabu.view.view.LoginView;
import com.zw.yzk.kabu.wxapi.WechatManager;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by zhanwei on 2016/12/23.
 */

public class LoginActivity extends BaseActivity implements LoginView {

    @Bind(R.id.phone_number)
    EditText phone;
    @Bind(R.id.password)
    EditText password;

    @Inject
    LoginPresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {

    }

    @Override
    public void initComponent() {
        DaggerLoginComponent
                .builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .loginModule(new LoginModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        phone.setText(SharedPrefrenceUtils.getString(this, SharedPrefrenceUtils.getLoginAccountKey()));
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
    }


    @OnClick(R.id.register)
    public void startRegisterPage() {
        startActivity(new Intent(this, RegisterActivity.class));
    }

    @OnClick(R.id.get_back_password)
    public void startForgetPasswordPage() {
        startActivity(new Intent(this, ForgetPasswordActivity.class));
    }

    @OnClick(R.id.login)
    public void login() {
        String phoneNum = phone.getText().toString();
        String passwordStr = password.getText().toString();
        if (TextUtils.isEmpty(phoneNum)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.please_enter_phone_number));
        } else if (TextUtils.isEmpty(passwordStr)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.please_enter_password));
        } else {
            //存储用户名
            SharedPrefrenceUtils.setValue(this, SharedPrefrenceUtils.getLoginAccountKey(), phoneNum);
            login(phoneNum, new GetRsaString(this, passwordStr).getRsa());
        }
    }

    @OnClick(R.id.wechat_login)
    public void loginByWechat() {
        WechatManager.loginbyWeichat(this);
    }

    @Override
    public void loginSuccess() {
        //初始化极光推送
        JPushHelper.init(this);
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    public void loginFail() {
        ToastManager.getInstance().showToast(this, getResources().getString(R.string.login_fail));
    }

    /**
     * 登录
     *
     * @param phone    手机号
     * @param token 密码和时间的rsa
     */
    private void login(String phone, String token) {
        presenter.login(phone, token);
        LoadingDialogManager.getInstance().showLoadingDialog(this);
    }
}
